package com.pet.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pet.server.domain.entity.Goods;
import com.pet.server.service.IGoodsService;
import com.pet.server.utils.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(name = "GoodsServlet", urlPatterns = "/goods")
public class GoodsServlet extends HttpServlet {
  @Resource private IGoodsService gs;

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    String method = request.getParameter("method");

    if (method == null || "".equalsIgnoreCase(method)) {
      getAllGoodsListByPage(request, response);
    } else if (method.equalsIgnoreCase("queryByGoodsName")) {
      findGoodsListByName(request, response);
    } else if (method.equalsIgnoreCase("queryByGoodsType")) {
      findGoodsListByGoodsType(request, response);
    } else if (method.equalsIgnoreCase("queryGoodsInfoById")) {
      findGoodsSelectiveById(request, response);
    } else if (method.equalsIgnoreCase("updateGoodsInfos")) {
      updateGoodsInfos(request, response);
    } else if (method.equalsIgnoreCase("deleteGoodsSelectiveById")) {
      deleteGoodsById(request, response);
    } else if (method.equalsIgnoreCase("add")) {
      addNewGoods(request, response);
    }
  }

  private void findGoodsListByGoodsType(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));

    int shopId = Integer.parseInt(request.getParameter("shopId"));
    String goodsType = request.getParameter("goodsType");

    Page<Goods> list = gs.getGoodsListByGoodsType(pageNo, pageSize, shopId, goodsType);

    ResultUtil<Page> r = null;
    if (list != null) {
      r = new ResultUtil<>(list);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void addNewGoods(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    Goods goods = parseDataFromJson(request, response);

    boolean b = gs.addNewGoods(goods);

    ResultUtil<Boolean> r = null;
    if (b) {
      r = new ResultUtil<>(true);
    } else {
      r = new ResultUtil<>("添加失败");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void deleteGoodsById(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    int goodsId = Integer.parseInt(request.getParameter("goodsId"));

    boolean b = gs.delGoodsById(goodsId);

    ResultUtil<Boolean> r = null;
    if (b) {
      r = new ResultUtil<>(true);
    } else {
      r = new ResultUtil<>("删除失败");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void updateGoodsInfos(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    Goods goods = parseDataFromJson(request, response);

    boolean b = gs.updateGoodsSelective(goods);
    // 向前端返回结果
    ResultUtil<Boolean> r = null;
    if (b) {
      r = new ResultUtil<>(true);
    } else {
      r = new ResultUtil<>("修改失败");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void findGoodsSelectiveById(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    int goodsId = Integer.parseInt(request.getParameter("goodsId"));
    //		int shopId = Integer.parseInt(request.getParameter("shopId"));

    Goods goods = gs.getGoodsSelectiveById(goodsId);

    ResultUtil<Goods> r = null;
    if (goods != null) {
      r = new ResultUtil<>(goods);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void findGoodsListByName(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));

    String goodsName = request.getParameter("goodsName");
    int shopId = Integer.parseInt(request.getParameter("shopId"));

    Page<Goods> list = gs.findGoodsListByName(pageNo, pageSize, shopId, goodsName);

    ResultUtil<Page> r = null;
    if (list != null) {
      r = new ResultUtil<>(list);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void getAllGoodsListByPage(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    int shopId = Integer.parseInt(request.getParameter("shopId"));

    Page<Goods> goodsList = gs.getAllGoodsList(pageNo, pageSize, shopId);
    ResultUtil<Page> r = null;
    if (goodsList != null) {
      r = new ResultUtil<>(goodsList);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /**
   * 通过Jackson的mapper方法 ,生成对象
   *
   * @param request req
   * @param response resp
   * @return 商品对象数据
   */
  private Goods parseDataFromJson(HttpServletRequest request, HttpServletResponse response) {
    // 声明传入数据类型
    response.setContentType("application/json");

    Goods goods = null;
    BufferedReader reader = null;
    // 获取reader
    try {
      reader = request.getReader();
      // 读取json数据流
      String json = reader.readLine();

      System.out.println("json = " + json);

      // 通过Jackson的Mapper方法反序列化,生成对象
      goods = new ObjectMapper().readValue(json, Goods.class);

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return goods;
  }
}
